# -*- coding:utf-8 -*-
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from . import db


class Role(db.Model):
    pass


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)
    _password = db.Column(db.String(100), nullable=False)
    createtime = db.Column(db.DateTime, default=datetime.now)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, rawpwd):
        self._password = generate_password_hash(rawpwd)

    def check_password(self, rawpwd):
        return check_password_hash(self.password, rawpwd)

    def __repr__(self):
        return '<User %r>' % self.username
